﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VVA.Data.Entity;
using VVA.Data.Service;
using VVA.Website.UI.Areas.Admin.Models.Product;

namespace VVA.Website.UI.Areas.Admin.Controllers
{
    [Authorize]
    public class ProductController : Controller
    {

        public ActionResult Index(int? page)
        {
            var service = new ProductService();
            var totalCount = 0;
            var model = service.GetAll("vi-VN", page.HasValue ? page.Value - 1 : 0, 10, out totalCount);
            ViewBag.TotalCount = totalCount;
            return View(model);
        }

        public ActionResult Create()
        {
            var model = new ProductDetailModel();
            return View(model);
        }

        [HttpPost]
        public ActionResult Create(Product model)
        {
            try
            {
                var service = new ProductService();
                model.Culture = "vi-VN";
                service.Insert(model);
                return new EmptyResult();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public ActionResult Edit(int id)
        {
            try
            {
                var service = new ProductService();
                var model = new ProductDetailModel();
                model.Model = service.GetByID(id);
                return View(model);
            }
            catch (Exception)
            {
                throw;
            }
        }

        [HttpPost]
        public ActionResult Edit(Product model)
        {
            try
            {
                var service = new ProductService();
                model.Culture = "vi-VN";
                service.Update(model);
                return new EmptyResult();
            }
            catch (Exception)
            {
                throw;
            }
        }

        [HttpPost]
        public ActionResult Delete(int id)
        {
            try
            {
                var service = new ProductService();
                service.Delete(new Product() { ProductID = id });
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return new EmptyResult();
        }
    }
}
